package com.ekeyman.securecredit.controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ekeyman.securecredit.dto.CardTransactionGrid;
import com.ekeyman.securecredit.dto.SearchGrid;
import com.ekeyman.securecredit.util.JSONUtil;
import com.ekeyman.securecreditlib.business.CardTransactionBO;
import com.ekeyman.securecreditlib.domain.CardTransaction;
import com.ekeymanlib.dto.SearchFilter;

@Controller
public class CardTransactionController {
	
	@Autowired
	private CardTransactionBO cardTransactionBO;

	@RequestMapping(method=RequestMethod.POST, value="/searchCardTransactions.sc" )
	public @ResponseBody SearchGrid searchCardTransactions(
			@RequestParam String id,
			@RequestParam String rows,
			@RequestParam String page,
			@RequestParam String sidx,
			@RequestParam String sord,
			@RequestParam(required=false) String filters,
			@RequestParam(required=false) String searchField,
			@RequestParam(required=false) String searchString,
			@RequestParam(required=false) String searchOper){
		
		/*
		 * id is the identifier of the parent CreditCard NOT the identifier of the CardTransaction
		 */
		SearchGrid searchCardTransaction = new SearchGrid();
		
		Integer intRows = new Integer(rows);
		
		Integer intPage = new Integer(page);
		searchCardTransaction.setPage(intPage);
		
		int to = (intRows * intPage);
	    int from = intPage - intRows;

		List<SearchFilter> searchFilters = JSONUtil.buildSearchFilter(filters);
		if(searchFilters == null){
			searchFilters = new ArrayList<SearchFilter>();
		}
		
		int records = ((Long)getCardTransactionBO().getCreditCardTransactionCount(id, searchFilters)).intValue();
		searchCardTransaction.setRecords(records);
		
		searchCardTransaction.setRows(getCardTransactionBO().gridCreditCardTransactionList(id, searchFilters, sidx, sord, from, to));
		
		searchCardTransaction.setTotal((int) Math.ceil((double) records / (double) intRows));

		return searchCardTransaction;
	}
	
	@RequestMapping(value="/editCardTransactions.sc", method=RequestMethod.POST)
	public @ResponseBody String editCardTransactions(
			@RequestParam String oper,
			@RequestParam String id,
			@RequestParam(required=false) String transactionNumber,
			@RequestParam(required=false) String vendorCode,
			@RequestParam(required=false) String transactionDate,
			@RequestParam(required=false) String postDate,
			@RequestParam(required=false) String amount,
			@RequestParam(required=false) String creditCardId){
		
		/*
		 * id is the identifier of the CardTransaction
		 */

		if (oper.equalsIgnoreCase("add")){
			CardTransaction cardTransaction = new CardTransaction();
			cardTransaction.setTransactionNumber(transactionNumber);
			cardTransaction.setVendorCode(vendorCode);
			cardTransaction.setTransactionDate(transactionDate);
			cardTransaction.setPostDate(postDate);
			cardTransaction.setAmount(new BigDecimal(amount));
			getCardTransactionBO().saveCardTransaction(creditCardId, cardTransaction);
		}
		else if (oper.equalsIgnoreCase("edit")) {
			long editId = Long.parseLong(id);
			CardTransactionGrid cardTransactionGrid = new CardTransactionGrid();
			cardTransactionGrid.setId(editId);
			cardTransactionGrid.setTransactionNumber(transactionNumber);
			cardTransactionGrid.setVendorCode(vendorCode);
			cardTransactionGrid.setTransactionDate(transactionDate);
			cardTransactionGrid.setPostDate(postDate);
			cardTransactionGrid.setAmount(amount);
			getCardTransactionBO().saveExistingCreditCardTransaction(cardTransactionGrid);
		}
		else if (oper.equalsIgnoreCase("del")){
	        StringTokenizer ids = new StringTokenizer(id, ",");
	        while (ids.hasMoreTokens())
	        {
	          long removeId = Long.parseLong(ids.nextToken());
	          getCardTransactionBO().deleteCardTransaction(removeId);
	        }
		}
		return id;
	}

	public void setCardTransactionBO(CardTransactionBO cardTransactionBO) {
		this.cardTransactionBO = cardTransactionBO;
	}

	public CardTransactionBO getCardTransactionBO() {
		return cardTransactionBO;
	}



}
